package com.landking.home.pic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.landking.R;
import com.landking.base.BaseFragment;
import com.landking.db.table.LK_PIC_M;
import com.landking.home.pic.adapter.PicItemAdapter;
import com.landking.home.pic.domain.PicStc;
import com.landking.http.HttpValues;
import com.landking.utils.FunUtil;
import com.landking.utils.PrefUtils;
import com.landking.view.loader.LatteLoader;

import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class PicListFragment extends BaseFragment implements View.OnClickListener {

    /**
     * UI
     */
    private View mContentView;
    private FrameLayout containerFl;
    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;

    private GridView mImageGridView;
    private LinearLayout emptyLl;

    private LK_PIC_M picM;// 需要改


    private String http_ip;

    List<PicStc> list = new ArrayList<PicStc>();

    public static PicListFragment newInstance() {
        Bundle args = new Bundle();
        PicListFragment fragment = new PicListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static PicListFragment newInstance(Bundle bundle) {
        Bundle args = bundle;
        PicListFragment fragment = new PicListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<PicListFragment> fragmentRef;

        public MyHandler(PicListFragment fragment) {
            fragmentRef = new SoftReference<PicListFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            PicListFragment fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();
            // 处理UI 变化
            switch (msg.what) {
                case 0:
                    //fragment.showAddProSuc(bundle);
                    break;
                case 1:
                    // fragment.showAdapter(bundle);
                    break;
                case 2:
                    // fragment.showTzAdapter(bundle);
                    break;
            }
        }
    }

    /**
     * data
     */
    public PicListFragment() {
    }

    // 获取传递过来的参数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            picM = (LK_PIC_M) arguments.get("LK_PIC_M");// 需要改
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();

        /*// 不重复加载 做缓存处理
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.fragment_piclist_layout, container, false);// 需要改
            this.initView();
            this.initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if (parent != null) {
            parent.removeView(mContentView);
        }*/

        mContentView = inflater.inflate(R.layout.fragment_piclist_layout, container, false);// 需要改
        this.initView();
        this.initData();

        return mContentView;
    }

    // 初始化视图
    private void initView() {
        containerFl = (FrameLayout) mContentView.findViewById(R.id.piclist_container);// 需要改
        backBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_back);
        confirmBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_confirm);
        backTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_back);
        titleTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_tv_title);
        confirmBtn.setVisibility(View.VISIBLE);
        backBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        titleTv.setOnClickListener(this);

        emptyLl = (LinearLayout) mContentView.findViewById(R.id.piclist_ll_empty);
        mImageGridView = (GridView) mContentView.findViewById(R.id.piclist_gv_gridView);
    }

    private void initData() {
        titleTv.setText(picM.getPictitle());//
        titleTv.setTextSize(14);
        http_ip = PrefUtils.getString(getActivity(), HttpValues.HTTP_IP, HttpValues.HTTP_IP_BASE);

        mImageGridView.setEmptyView(emptyLl);

        int end = picM.getEnd();

        list.clear();
        for (int i = 1; i <= end; i++) {
            String picurl = http_ip + picM.getPicpath() + picM.getPictype() + "/" + picM.getPictitle() + "/" + FunUtil.getSeqNumByLong(i, 3) + picM.getSuffix();
            String picname = FunUtil.getSeqNumByLong(i, 3) + picM.getSuffix();
            list.add(new PicStc(picurl, picname));
        }

        PicItemAdapter adapter = new PicItemAdapter(getActivity(), list);
        mImageGridView.setAdapter(adapter);

        mImageGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LatteLoader.showLoading(getActivity());

                Intent intent = new Intent(getActivity(), PicViewPagerActivity.class);
                intent.putExtra("images", (Serializable) list);
                intent.putExtra("position", position);
                startActivity(intent);

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_navigation_rl_back://  // 需要改

                getFragmentManager().popBackStack();

                /*LowFragment fragment2 = new LowFragment();
                if (fragment2 != null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("title", "LowFragment2");
                    fragment2.setArguments(bundle);
                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    transaction.replace(R.id.low_container, fragment2);// 需要改
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.addToBackStack(null);
                    transaction.commit();
                }*/
                break;
        }
    }
}